﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WebAddressBook
{
    public partial class ManageDept : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitDDlEnterprise();
                InitDDLFatherDept();
                InitDDlEnterprise2();
                InitLBDeptList();
            }
        }

        protected void BtCreate_Click(object sender, EventArgs e)
        {
            //TODO:字段不为空值的判断，在前端做，还没做的
            Adrr.BLL.Base_Dept baseDeptBll = new Adrr.BLL.Base_Dept();
            Adrr.Model.Base_Dept baseDeptModel = new Adrr.Model.Base_Dept();

            //前面4位表示企业id,之后的四位表示部门id
            string enterpriseId = DDLEnterprise.SelectedValue;
            //若未创建有企业，那么不可以进行创建部门的工作
            if (string.IsNullOrEmpty(enterpriseId))
            {
                LbMessage.Text = "<div class='box box-error'>创建失败，请先去添加企业，然后才能创建部门！</div>";
                return;
            }

            if (!VerifyInput())
            {
                LbMessage.Text = "<div class='box box-error'>创建失败，带有 * 的字段不能为空！</div>";
                return;
            }
            string strWhere = "";
            string nowRow = "";
            string deptId = "";
            if (string.IsNullOrEmpty(DDLFatherDept.SelectedValue))
            {
                strWhere = "DeptID like '" + enterpriseId + "%' and len(DeptID)=8";
                nowRow = (baseDeptBll.GetRecordCount(strWhere) + 1).ToString();
                deptId = enterpriseId + nowRow.PadLeft(4, '0');//不够位数，前面补零
            }
            else
            {
                strWhere = "DeptID like '" + DDLFatherDept.SelectedValue + "%' and len(DeptID)=" + (DDLFatherDept.SelectedValue.Length + 4);
                nowRow = (baseDeptBll.GetRecordCount(strWhere) + 1).ToString();
                deptId = DDLFatherDept.SelectedValue + nowRow.PadLeft(4, '0');//不够位数，前面补零
            }

            //begin 组织model
            baseDeptModel.DeptID = deptId;
            baseDeptModel.DeptName = deptName.Value.Trim();
            baseDeptModel.DeptBriefName = deptShortName.Value.Trim();
            baseDeptModel.IsWorkDress = Convert.ToBoolean(isWorkAddress.SelectedValue);
            baseDeptModel.PhoneNum = phoneNum.Value.Trim();
            baseDeptModel.CreateTime = DateTime.Now;
            baseDeptModel.UpdateTime = DateTime.Now;
            baseDeptModel.isDelete = false;
            baseDeptModel.PinYin = deptPinYin.Value.Trim();
            //end

            if (baseDeptBll.Add(baseDeptModel))
            {
                LbMessage.Text = "<div class='box box-info'>添加成功！</div>";
                ClearUIValue();
                RefreshAllData();
            }
            else
                LbMessage.Text = "<div class='box box-error'>添加失败！</div>";

        }
        /// <summary>
        /// 刷新数据
        /// </summary>
        private void RefreshAllData()
        {
            InitDDLFatherDept();
            InitLBDeptList();
        }

        private void InitDDlEnterprise()
        {
            DataSet ds = CommonHelper.GetEnterprise();
            DDLEnterprise.DataSource = ds;
            DDLEnterprise.DataValueField = "DeptID";
            DDLEnterprise.DataTextField = "DeptName";
            DDLEnterprise.DataBind();
        }

        private void InitDDLFatherDept()
        {
            DataTable dt = CommonHelper.GetDeptList(DDLEnterprise.SelectedValue);
            DDLFatherDept.DataSource = dt;
            DDLFatherDept.DataValueField = "DeptID";
            DDLFatherDept.DataTextField = "DeptName";
            DDLFatherDept.DataBind();

            DDLFatherDept.Items.Add(new ListItem("直属企业", ""));
            DDLFatherDept.SelectedValue = "";
        }

        private void InitDDlEnterprise2()
        {
            DataSet ds = CommonHelper.GetEnterprise();
            DDLEnterprise2.DataSource = ds;
            DDLEnterprise2.DataValueField = "DeptID";
            DDLEnterprise2.DataTextField = "DeptName";
            DDLEnterprise2.DataBind();
        }

        private void InitLBDeptList()
        {
            string enterpriseId = DDLEnterprise2.SelectedValue;
            //DataSet ds = CommonHelper.GetDept(enterpriseId);
            DataTable dt = CommonHelper.GetDeptList(enterpriseId);
            LBDeptList.DataSource = dt;
            LBDeptList.DataValueField = "DeptID";
            LBDeptList.DataTextField = "DeptName";
            LBDeptList.DataBind();
        }
        /// <summary>
        /// 清空ui上面的值
        /// </summary>
        private void ClearUIValue()
        {
            deptName.Value = "";
            deptShortName.Value = "";
            deptPinYin.Value = "";
            phoneNum.Value = "";
        }

        protected void DDLEnterprise2_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitLBDeptList();
        }

        protected void BtDelete_Click(object sender, EventArgs e)
        {
            if (LBDeptList.SelectedIndex == -1)
                return;
            string deptId = LBDeptList.SelectedValue;
            Adrr.BLL.Base_Dept baseDept = new Adrr.BLL.Base_Dept();
            try
            {
                if (baseDept.SetThisDeptIsDeleted(deptId))
                    UpperThisDeptUser(deptId);
            }
            catch (Exception ex)
            {
 
            }
            //刷新 
            RefreshAllData();
        }
        /// <summary>
        /// 把删除的这个部门下面的所有联系人提升到被删除的这个部门的上一级部门
        /// </summary>
        private void UpperThisDeptUser(string delDeptId)
        {
            //上一级部门的id
            string upperDeptId = delDeptId.Substring(0, delDeptId.Length - 4);
            Adrr.BLL.Base_Employee baseEmployeeBll = new Adrr.BLL.Base_Employee();

            baseEmployeeBll.UpperEmployeeDept(delDeptId, upperDeptId);
        }
        protected void DDLEnterprise_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitDDLFatherDept();
        }

        private bool VerifyInput()
        {
            if (string.IsNullOrEmpty(deptName.Value.Trim()) || string.IsNullOrEmpty(deptShortName.Value.Trim())
                || string.IsNullOrEmpty(deptPinYin.Value.Trim()))
                return false;
            else if (isWorkAddress.SelectedValue == "True")
            {
                if (!string.IsNullOrEmpty(phoneNum.Value.Trim()))
                    return true;
                else
                    return false;
            }
            else
                return true;
        }
    }
}